package org.redr.canvas;

import java.lang.reflect.Field;
import java.util.Vector;

import org.apache.commons.lang3.StringUtils;

public class WidgetRegistry extends Registry{
	public String forName;
	
	public WidgetRegistry(String n, String i, String t, String summ, String det, String cit, String fName){
		
		name = n;
		icon = i;
		tags = new Vector<String>();
		for(String tag : StringUtils.split(t, ";")){
			tags.add(tag);
		}
		summary = summ;
		details = det;
		citation = cit;
		forName = fName;
	}
	
	public WidgetRegistry(String n){
		name = n;
		icon = "default.png";
		tags = new Vector<String>();
		summary = "";
		details = "";
		citation = "";
	}

	public WidgetRegistry(Class<?> c) throws Exception{
		// TODO Auto-generated constructor stub
		System.out.println("Registering widget: " + c.toString());
		Field name = c.getField("name");
		// System.out.println("Name field is " + name.toGenericString());
		this.name = name.get(null).toString();
		
		try {
			this.citation = c.getField("citation").get(null).toString();
		} catch(NoSuchFieldException e){
		
		} catch (Exception ecitation) {
			ecitation.printStackTrace();
		}
		
		try {
			this.details = c.getField("details").get(null).toString();
		} catch(NoSuchFieldException e){
			
		} catch (Exception edetails) {
			edetails.printStackTrace();
		}
		
		try {
			for (String t : c.getField("tags").get(null).toString().split("::")){
				tags.add(t);
			}
		} catch(NoSuchFieldException e){
			
		} catch (Exception etag) {
			etag.printStackTrace();
		}
		
		try {
			this.summary = c.getField("summary").get(null).toString();
		} catch(NoSuchFieldException e){
			
		} catch (Exception edetails) {
			edetails.printStackTrace();
		}
		
		try {
			this.icon = c.getField("icon").get(null).toString();
		} catch(NoSuchFieldException e){
			
		} catch (Exception edetails) {
			edetails.printStackTrace();
			this.icon = "default.png";
		}
	}
	
	@Override
	public int type(){
		return 1;
	}
}
